home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / utilitys / sman_1_1 / part01 / src / sless_1.c < prev    next >
C/C++ Source or Header  |  1991-07-29  |  7KB  |  405 lines

  1.  
  2. #include "sless_defs.h"
  3. #include <libraries/sregexpbase.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <exec/execbase.h>
  8. #include <clib/dos_protos.h>
  9. #include <clib/exec_protos.h>
  10. #include <clib/sregexp_protos.h>
  11.  
  12. Prototype void            backup(int);
  13. Prototype char *        getbackline(int *);
  14. Prototype int            displines(char *);
  15. Prototype char *        skipdispline(char *);
  16. Prototype void            downone(void);
  17. Prototype long            skiponeline(long);
  18. Prototype int            breakfn(void);
  19. Prototype void            bottomline(void);
  20. Prototype int            searchfor(void);
  21. Prototype void            nextpat(void);
  22. Prototype void            prevpat(void);
  23. Prototype void            behelpfull(void);
  24. Prototype void            chkabort(void);
  25. Prototype void            uponeline(void);
  26.  
  27. extern const char memory[],nolast[],nopat[],wait[],abort[];
  28.  
  29. extern char seek;
  30. extern long startpos,nextpos;
  31. extern FILE *file,*ofile;
  32. extern BPTR con;
  33. extern int sx,sy;
  34. extern struct SregExp *lastspat;
  35.  
  36. extern struct ExecBase *SysBase;
  37.  
  38. void
  39. backup(n)
  40. int n;
  41. {
  42.     char *p,*q,*h;
  43.     int l = 0;
  44.  
  45.     show_line;
  46.     do {
  47.     if (l)
  48.         free(p);
  49.     if (!(p = getbackline(&startpos)))
  50.         break;
  51.     l += displines(p);
  52.     } while (l < n);
  53.  
  54.     h = p;
  55.     while (l > n) {
  56.     q = skipdispline(p);
  57.     startpos += q - p;
  58.     p = q;
  59.     l-- ;
  60.     }
  61.     if (h)
  62.     free(h);
  63.     refreshscreen();
  64. }
  65.  
  66. char *
  67. getbackline(pos)
  68. long *pos;
  69. {
  70.     char *p,c;
  71.     int l = 0;
  72.  
  73.     show_line;
  74.     if (*pos == 0)
  75.     return NULL;
  76.     sseekbot(file,*pos-1);
  77.     while (*pos > 0) {
  78.     c = sgetcur(file);
  79.     if (c == '\n' && l != 0) {
  80.         sseek(file,+1,SEEK_CUR);
  81.         break;
  82.     }
  83.     sseek(file,-1,SEEK_CUR);
  84.     l++;
  85.     (*pos)--;
  86.     }
  87.     if (!(p = malloc(l+1)))
  88.     leave(12,memory);
  89.     sgets(p,l+1,file);
  90.     return p;
  91. }
  92.  
  93. int
  94. displines(p)
  95. char *p;
  96. {
  97.     int l = 1;
  98.  
  99.     show_line;
  100.     while (*(p = skipdispline(p))) l++;
  101.     return l;
  102. }
  103.  
  104. char *
  105. skipdispline(p)
  106. char *p;
  107. {
  108.     int l = 0;
  109.  
  110.     show_line;
  111.     while (*p && l < sx) {
  112.     switch (*p++) {
  113.         case (12) :
  114.         return (l + 2 <= sx) ? p : p - 1;
  115.         case ((char)0x1b) :
  116.         if (*p != '[')
  117.             break;
  118.         p++;
  119.         case ((char)0x9b) :
  120.         while (isdigit(*p) || *p == ';' || *p == ' ')
  121.             p++;
  122.         p++;
  123.         break;
  124.         case (9) :
  125.         l = (l + 8) & (~7);
  126.         if (l > sx)
  127.             return p-1;
  128.         break;
  129.         case ('\n') :
  130.         return p;
  131.         default :
  132.         l++;
  133.     }
  134.     }
  135.     while ((*p == 0x1B && *(p+1) == '[') || *p == (char)0x9b) {
  136.     if (*p == 0x1b)
  137.         p++;
  138.     do {
  139.         p++;
  140.     } while (isdigit(*p) || *p == ';' || *p == ' ');
  141.     p++;
  142.     }
  143.     if (*p == '\n')
  144.     p++;
  145.     return p;
  146. }
  147.  
  148. void
  149. downone()
  150. {
  151.     int pos;
  152.  
  153.     show_line;
  154.     if (seek) {
  155.     pos = stell(file);
  156.     startpos = skiponeline(startpos);
  157.     sseek(file,pos,SEEK_SET);
  158.     }
  159.     bottomline();
  160.     if (seek)
  161.     nextpos = stell(file);
  162.     putline();
  163.     if (seof(file))
  164.     nextpos = -1;
  165.     putconstr(": ",con);
  166. }
  167.  
  168. long
  169. skiponeline(pos)
  170. long pos;
  171. {
  172.     char *p,*q;
  173.  
  174.     show_line;
  175.     sseek(file,pos,SEEK_SET);
  176.     if (!(p = sgetline(file)))
  177.     return pos;
  178.     q = skipdispline(p);
  179.     free(p);
  180.     return pos + q - p;
  181. }
  182.  
  183. void
  184. uponeline()
  185. {
  186.     char *p,*h,*q;
  187.     int l,j;
  188.     long t;
  189.  
  190.     h = p = getbackline(&startpos);
  191.     l = displines(p);
  192.     while (l > 1) {
  193.     q = skipdispline(p);
  194.     startpos += q - p;
  195.     p = q;
  196.     l--;
  197.     }
  198.     free(h);
  199.     if (nextpos < 0) {
  200.     sseek(file,startpos,SEEK_SET);
  201.     l = 0;
  202.     while ((p = sgetline(file)) && (l += displines(p)) < sy - 2) {
  203.         free(p);
  204.     }
  205.     if (p) {
  206.         nextpos = stell(file);
  207.         if (l > sy - 2) {
  208.         j = displines(p);
  209.         q = p;
  210.         while (l - j < sy - 2) {
  211.             q = skipdispline(q);
  212.             j--;
  213.         }
  214.         nextpos -= p + strlen(p) - q;
  215.         }
  216.         free(p);
  217.     } else
  218.         nextpos = -1;
  219.     } else {
  220.     h = p = getbackline(&nextpos);
  221.     l = displines(p);
  222.     while (l > 1) {
  223.         q = skipdispline(p);
  224.         nextpos += q - p;
  225.         p = q;
  226.         l-- ;
  227.     }
  228.     free(h);
  229.     }
  230.     sseek(file,startpos,SEEK_SET);
  231.     putconstr("\x9b0 p\x9bT\x9b0;0;H",con);
  232.     putline();
  233.     bottomline();
  234.     putconstr(": \x9b p",con);
  235.     t = skiponeline(nextpos);
  236.     sseek(file,t,SEEK_SET);
  237. }
  238.  
  239. int
  240. breakfn()
  241. {
  242.     show_line;
  243.     if (con) {
  244.     while (WaitForChar(con,100))
  245.         rawreadcon(con);
  246.     }
  247.     leave(5,"***BREAK***\n");
  248. }
  249.  
  250. void
  251. bottomline()
  252. {
  253.     show_line;
  254.     putconchar('\x9b',con);
  255.     putconint(sy,con);
  256.     putconstr(";0H\x9bK",con);
  257. }
  258.  
  259. int
  260. searchfor()
  261. {
  262.     char *pat,buff[204];
  263.     struct SregExp *spat;
  264.  
  265.     show_line;
  266.     if (!(pat = getstr(con))) {
  267.     bottomline();
  268.     putconstr(": ",con);
  269.     return FALSE;
  270.     }
  271.     if (!IsWild(pat)) {
  272.     strcpy(buff,"#?");
  273.     strcat(buff,pat);
  274.     strcat(buff,"#?");
  275.     } else {
  276.     strcpy(buff,pat);
  277.     strcat(buff,"\n");
  278.     }
  279.     free(pat);
  280.     if (!(spat = ParseSregExp(buff)))
  281.     return FALSE;
  282.     if (lastspat)
  283.     FreeSregExp(lastspat);
  284.     lastspat = spat;
  285.     return TRUE;
  286. }
  287.  
  288. void
  289. nextpat()
  290. {
  291.     long h,i;
  292.     char *p;
  293.     struct Task *task = SysBase->ThisTask;
  294.  
  295.     show_line;
  296.     if (!lastspat) {
  297.     bottomline();
  298.     putconstr(nolast,con);
  299.     return;
  300.     }
  301.     bottomline();
  302.     putconstr(wait,con);
  303.     h = startpos;
  304.     i = stell(file);
  305.     sseek(file,startpos,SEEK_SET);
  306.     p = sgetline(file);
  307.     free(p);
  308.     startpos = stell(file);
  309.     while (p = sgetline(file)) {
  310.     if (MatchSregExp(p,lastspat,FALSE)) {
  311.         free(p);
  312.         refreshscreen();
  313.         return;
  314.     }
  315.     free(p);
  316.     startpos = stell(file);
  317.     if (task->tc_SigRecvd & SIGBREAKF_CTRL_C) {
  318.         SetSignal(0, SIGBREAKF_CTRL_C);
  319.         bottomline();
  320.         putconstr(abort,con);
  321.         goto done;
  322.     }
  323.     }
  324.     bottomline();
  325.     putconstr(nopat,con);
  326. done:
  327.     startpos = h;
  328.     sseek(file,i,SEEK_SET);
  329. }
  330.  
  331. void
  332. prevpat()
  333. {
  334.     long h,i;
  335.     char *p;
  336.     struct Task *task = SysBase->ThisTask;
  337.  
  338.     show_line;
  339.     if (!lastspat) {
  340.     bottomline();
  341.     putconstr(nolast,con);
  342.     return;
  343.     }
  344.     bottomline();
  345.     putconstr(wait,con);
  346.     h = startpos;
  347.     i = stell(file);
  348.     while (p = getbackline(&startpos)) {
  349.     if (MatchSregExp(p,lastspat,FALSE)) {
  350.         free(p);
  351.         refreshscreen();
  352.         return;
  353.     }
  354.     free(p);
  355.     if (task->tc_SigRecvd & SIGBREAKF_CTRL_C) {
  356.         SetSignal(0, SIGBREAKF_CTRL_C);
  357.         bottomline();
  358.         putconstr(abort,con);
  359.         goto done;
  360.     }
  361.     }
  362.     bottomline();
  363.     putconstr(nopat,con);
  364. done:
  365.     startpos = h;
  366.     sseek(file,i,SEEK_SET);
  367. }
  368.  
  369. void
  370. behelpfull()
  371. {
  372.     char oseek;
  373.     long ostartpos,onextpos;
  374.  
  375.     show_line;
  376.     ofile = file;
  377.     oseek = seek;
  378.     ostartpos = startpos;
  379.     onextpos = nextpos;
  380.     seek = TRUE;
  381.     file = sopen(NULL);
  382.     startpos = 0;
  383.     refreshscreen();
  384.     waitcommand();
  385.     sclose(file);
  386.     file = ofile;
  387.     ofile = NULL;
  388.     seek = oseek;
  389.     startpos = ostartpos;
  390.     nextpos = onextpos;
  391.     refreshscreen();
  392. }
  393.  
  394. void
  395. chkabort(void)
  396. {
  397.     struct Task *task = SysBase->ThisTask;
  398.  
  399.     if (task->tc_SigRecvd & SIGBREAKF_CTRL_C) {
  400.     SetSignal(0, SIGBREAKF_CTRL_C);
  401.     breakfn();
  402.     }
  403. }
  404.  
  405.